<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<html>
<head>
    <title>管理员管理歌曲信息</title>
    <script src="${pageContext.request.contextPath}/js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.form.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/musicList.css">
    <%
        String url = request.getContextPath() + "/deleteMusicServlet?musicName=";
        String  illegalFile = (String) session.getAttribute("illegalFile");
        String  deleteSuccess = (String) session.getAttribute("deleteSuccess");
    %>
    <script>
        <%if(illegalFile != null){%>
            alert("${illegalFile}");
        <%
             session.removeAttribute("illegalFile");
        }%>
        <%if(deleteSuccess != null){%>
            alert("${deleteSuccess}");
        <%
            session.removeAttribute("deleteSuccess");
        }%>
    </script>
    <script>
        // window.onload=function () {
        //     refresh();
        // };
        function refresh() {
            $.ajax({
                type:"POST",
                url:"${pageContext.request.contextPath}"+"/getAllMusic"
            })
        }
        function getXMLHttpRequest() {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            return xmlhttp;
        }
        //更新数据库
        function update() {
            var req1=getXMLHttpRequest();
            req1.onreadystatechange=function () {
                if(req1.readyState===4){
                    // alert(req.status);//查看服务器端响应状态
                    if(req1.status===200){
                        var text = eval("("+req1.responseText+")");
                        if(text.result==="true"){
                            window.location.href="/WangWoMusic/musicList";
                        }else{
                            window.location.href="/WangWoMusic/musicList";
                        }
                    }
                }
            };
            //2.建立一个连接
            req1.open("POST","/WangWoMusic/updateDB");
            //设置请求头
            req1.setRequestHeader("Content-type","application/json");
            //3.发送请求
            req1.send();
        }

        //上传
        function upload() {
            var form = document.getElementById("form1");
            // var fd = new FormData(form);
            var option=({
                type:"POST",
                url:"/WangWoMusic/uploadMusic",
                data:{
                    file:"file1"
                },
                dataType:"text",
                complete:function (data) {
                    var text = eval("("+data+")");
                    alert(text.result);
                    if(text.result==="true"){
                        alert("上传成功");
                        // window.location.href="/WangWoMusic/musicList";
                    }else{
                        alert(text.msg);
                    }
                }
            });
            $("#form1").ajaxSubmit(option)
        //     var form = document.getElementById("form1");
        //     var fd = new FormData(form);
        //
        //     var req=getXMLHttpRequest();
        //     req.onreadystatechange=function () {
        //         // alert(req.readyState);
        //         if(req.readyState===4){
        //             alert(req.status);//查看服务器端响应状态
        //             if(req.status===200){
        //                 var text = eval("("+req.responseText+")");
        //                 alert(text.result);
        //                 if(text.result==="true"){
        //                     alert("上传成功");
        //                     // window.location.href="/WangWoMusic/musicList";
        //                 }else{
        //                     alert(text.msg);
        //                 }
        //             }
        //         }
        //     };
        //     req.open("POST","/WangWoMusic/uploadMusic");
        //     // req.setRequestHeader("Content-type","application/json");
        //     req.send(fd);
        }
    </script>
</head>
<body>
<div id="uploadMusic">
    <a style="display: block;position: absolute;left: 150px;" href="${pageContext.request.contextPath}/logout">注销</a>
    <h3>管理员上传文件</h3>
    <%--${pageContext.request.contextPath}/uploadMusic--%>
    <form id="form1" enctype="multipart/form-data" method="post">
        <p>请选择文件</p>
        <input type="file" name="file1" id="fileUpload" multiple="multiple" accept="audio/mpeg"><br>
        <input type="submit" onclick="upload()" value="上传">
        &nbsp;&nbsp;
        <input type="button" onclick="update()" value="更新数据库"><br/>
    </form>
</div>
<div id="music_list" width="500px">
    <table border="1">
        <tr>
            <th>歌名</th>
            <th>歌手</th>
            <th>歌曲信息</th>
            <th>删除</th>
        </tr>
        <c:forEach var="musicInfo" items="${sessionScope.musicList}">
            <tr>
                <td>${musicInfo.musicName}</td>
                <td>${musicInfo.singer}</td>
                <td>${musicInfo.musicInfo}</td>
                <td><a href="${pageContext.request.contextPath}/deleteMusic?musicName=${musicInfo.musicPath}">删除</a></td>
        </c:forEach>
    </table>
</div>

</body>
</html>
